package com.example.dee.controller;

import com.example.dee.pojo.CarData;
import com.example.dee.pojo.User;
import com.example.dee.service.LoginService;
import com.example.dee.service.SysService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
public class LoginController {
    @Resource
    LoginService loginService;
    @Resource
    SysService sysService;

    @RequestMapping("/exit")
    public String exit(HttpServletRequest request){
        request.getSession().removeAttribute("sysUser");
        return "login";
    }
    @RequestMapping("/")
    public String run(HttpServletRequest request, HttpServletResponse response){
         return "login";
    }
    @RequestMapping("/register")
    public String register(HttpServletRequest request, HttpServletResponse response){
        return "register";
    }
    @RequestMapping("/login")
    public String login(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        System.out.print(username);
        System.out.print(password);
        String passwordTrue= loginService.selectPassword(username);
        if(passwordTrue!=null){
            if(passwordTrue.equals(password)){
                //登录成功：获取用户信息，进入主页
                User user=new User();
                user.setUserName(username);
                user.setPassword(password);
                request.getSession().setAttribute("sysUser", user);
                List<CarData> carDataList = sysService.selectCarDataByUp(0,50);
                request.setAttribute("begin","50");
                request.setAttribute("carDataList",carDataList);
                return "carDataUp";
            }else {
                //登录失败：页面提示，重新登录
                response.getWriter().write("<script>alert('密码错误');</script>");
                return "login";
            }
        }else{
            //登录失败：页面提示，未注册
            response.getWriter().write("<script>alert('用户未注册');</script>");
            return "login";
        }
    }

    @RequestMapping("/regist")
    public String regist(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String username=request.getParameter("username")==null?"":request.getParameter("username");
        String password=request.getParameter("password")==null?"":request.getParameter("password");
        String name=request.getParameter("name")==null?"":request.getParameter("name");
        int role=0;
        if(username.equals("")||password.equals("")||name.equals("")){
            //注册失败
            response.getWriter().write("<script>alert('信息输入不完全！');</script>");
            return "register";
        }else{
            int checkuser = loginService.checkUser(username);
            //若用户名不存在，执行插入操作
            if (checkuser == 0) {
                User user = new User();
                user.setUserName(username);
                user.setPassword(password);
                user.setName(name);
                user.setRole(role);
                Boolean flag = loginService.regist(user);//此处为service中的方法，往数据库插入人员信息
                if (flag == true) {
                    return "login";
                } else {
                    //注册失败
                    response.getWriter().write("<script>alert('注册失败');</script>");
                    return "register";
                }
            } else {
                response.getWriter().write("<script>alert('用户名已存在');</script>");
                return "register";
            }
        }


    }
}
